package com.wc.算法基础课.B第二讲数据结构.并查集.连通块中点的数量;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/20 12:26
 * @description https://www.acwing.com/problem/content/839/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010;
    static int[] a = new int[N];
    static int[] siz = new int[N];
    static int n, q, x, y;

    public static void main(String[] args) {
        n = sc.nextInt();
        q = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i] = i;
            siz[i] = 1;
        }
        while (q-- > 0) {
            String op = sc.next();
            x = sc.nextInt();
            if (op.equals("C")) {
                y = sc.nextInt();
                if (find(x) != find(y)) {
                    merge(x, y);
                }
            } else if (op.equals("Q1")) {
                y = sc.nextInt();
                if (find(x) == find(y)) {
                    out.println("Yes");
                } else {
                    out.println("No");
                }
            } else {
                out.println(siz[find(x)]);
            }
        }
        out.flush();
    }

    static int find(int x) {
        if (a[x] != x) a[x] = find(a[x]);
        return a[x];
    }

    static void merge(int x, int y) {
        int xx = find(x);
        int yy = find(y);
        a[xx] = yy;
        siz[yy] += siz[xx];
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}